const app = getApp();
Page({
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    hidden: true,
    action: null,
  },
  onLoad(options) {
    console.log(options)
    let users = wx.getStorageSync('users');
    this.setData({
      users: users,
      list: users,
    })
    if (options.action) {
      this.setData({
        action: options.action
      });
    }
  },
  onReady() {
    let that = this;
  },
  Search(e) {
    var key_word = e.detail.value;
    let users = this.data.users;
    let list = [];
    for (let i = 0; i < users.length; i++) {
      if (users[i].name.indexOf(key_word) != -1) {
        list.push(users[i]);
      }
    }
    this.setData({
      list: list,
    })
  },
  Select(e) {
    let id = e.currentTarget.id;
    if (!this.data.action) {
      wx.setStorageSync('tmp_user', id);
      wx.navigateBack({
        delta: 1,
      })
      return;
    }
    if (this.data.action == 'login') {
      wx.setStorageSync('user_id', id);
      let users = this.data.users;
      for (let i = 0; i < users.length; i++) {
        console.log(users[i].user_id);
        console.log(id);
        if (users[i].user_id == id) {
          wx.setStorageSync('user_name', users[i].name);
          wx.navigateBack({
            delta: 1,
          })
          break;
        }
      }
    }
  }
});